﻿namespace DataScripter.Excel.Formulas
{
	class AbsoluteReference : Formula
	{
		private readonly int _rowDiff;
		private readonly int _colDiff;
		private readonly ICell _cell;
		private readonly string _cellReference;

		public AbsoluteReference(int rowDiff, int colDiff)
		{
			_rowDiff = rowDiff;
			_colDiff = colDiff;
		}

		public AbsoluteReference(ICell cell)
		{
			_cell = cell;
		}

		public AbsoluteReference(string cellReference)
		{
			_cellReference = cellReference;
		}

		public ICell Cell
		{
			get { return _cell; }
		}

		#region Overrides of Formula

		public override string Resolve(ICell current)
		{
			if (_cellReference != null)
				return _cellReference;
			if (_cell != null)
				return _cell.AbsoluteCellReference;
			return current.GetDiff(_rowDiff, _colDiff).AbsoluteCellReference;
		}

		#endregion

		public override string ToString()
		{
			string format = string.Format("({0}, {1})", _rowDiff, _colDiff);
			return _cellReference ?? (_cell == null ? format : _cell.AbsoluteCellReference);
		}
	}
}